Recall the histogram for cut categories from the diamonds data set:
library(plotly)
library(tidyverse)
diamonds %>%
plot_ly( x = ~cut, showlegend = FALSE ) %>%
add_histogram(hoverinfo = "x+y") %>%
dplyr::group_by( cut ) %>%
summarise(n = n()) %>%
add_text(
text = ~scales::comma(n), y = ~n,
textposition = "top middle",
cliponaxis = FALSE,
hoverinfo = "none"
)
Please add to it the following changes:
clarity variable."Blues" palette.hoverlabel)."pink"."lightgray".span) of 1."green"."Cut" (instead of
“cut”)."darkred" and its
font to "Times New Roman"."darkorange"
(be careful not to change other things with it)."red". No
tooltip is associated with these values."Ugly, ugly plot" in the
"Old Standard TT" font at the top left corner.In addition, make sure that the data that plot_ly
utilizes is the minimum data necessary for the plot.
Notes:
"Pretty ugly plot",
which I applied here instead of "Ugly, ugly plot".total_counts <- diamonds %>%
group_by(cut) %>%
summarise(total = n())
diamonds_summary <- diamonds %>%
count(cut, clarity)
p <- diamonds %>%
count(cut, clarity) %>% # Count the number of diamonds for each combination of cut and clarity
plot_ly() %>%
add_bars(
x = ~cut,
y = ~n,
color = ~clarity,
colors = "Blues",
hoverinfo = 'text',
text = ~paste("The cut is", cut, "\nthe clarity", clarity, "\nand there are", n, "such diamonds!"),
marker = list(line = list(color = 'black', width = 1)), # Set the borders of the bars to black with a thickness of 1
showlegend = FALSE,
textposition = "none"
) %>%
layout(
barmode = 'stack',
hoverlabel = list(bgcolor = "yellow", font = list(color = "red")), # Set tooltip text color to red and background to yellow
paper_bgcolor = "pink", # Set the background color of the entire plot to pink
plot_bgcolor = "lightgray", # Set the background color of the plotting area to lightgray
xaxis = list(
tickfont = list(color = "green"), # Set the color of the x-axis labels to green
title = "Cut", # Set the title of the x-axis to "Cut"
titlefont = list(color = "darkred", family = "Times New Roman") # Set the color and font of the x-axis title
),
yaxis = list(tickfont = list(color = "darkorange"), title = list(text = "")), # Set the color of the y-axis labels to darkorange and remove the y-axis title
margin = list(l = 25), # Set the left margin of the plot to 25px
title = list(text = "Pretty ugly plot", font = list(family = "Old Standard TT"), x = 0, y = 1) # Add the title "Ugly, ugly plot" in the "Old Standard TT" font at the top left corner
)
# Add the text annotations for total counts
for (i in 1:nrow(total_counts)) {
p <- p %>% add_text(
data = total_counts[i, ],
x = ~cut,
y = ~total,
text = ~total,
textposition = 'top middle',
hoverinfo = 'none',
showlegend = FALSE,
textfont = list(color = "red")
)
}
p